package blog.servlet;

import blog.dao.ArticleDao;
import blog.model.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;

@WebServlet("/edit.do")
public class EditDoServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String title = req.getParameter("title");
        String type = req.getParameter("type");
        String content = req.getParameter("editor-markdown-doc");

        User user = null;
        HttpSession session = req.getSession(false);
        if (session != null) {
            user = (User) session.getAttribute("currentUser");
        }

        if (user == null) {
            resp.sendRedirect("/login.html");
            return;
        }

        ArticleDao articleDao = new ArticleDao();
        int aid = articleDao.insert(user.uid,title,type,new Date(), content);

        resp.sendRedirect("/blog_content.html?aid=" + aid);
    }
}
